﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using IConfig;

namespace VideoConfig
{
    public class FileConfigReader : IConfigReader
    {
        private static IConfigReader _config;

        private readonly string path = ".\\config";

        private readonly Dictionary<string, Dictionary<string, FileConfigInfo>> configInfos;

        private readonly string areaKey = "area";
        private readonly string deviceKey = "device";

        private FileConfigReader()
        {
            DirectoryInfo dirConfig = new DirectoryInfo(path);
            if (dirConfig.Exists)
            {
                this.configInfos = new Dictionary<string, Dictionary<string, FileConfigInfo>>();

                foreach (FileInfo FI in dirConfig.GetFiles("*.config"))
                {
                    //创建配置文件
                    FileConfigInfo configInfo = new FileConfigInfo(FI);
                    string area = configInfo[areaKey];
                    string device = configInfo[deviceKey];
                    if (area != string.Empty
                        && device != string.Empty)
                    {
                        if (!this.configInfos.ContainsKey(area))
                        {//添加区域配置列表
                            this.configInfos.Add(area, new Dictionary<string, FileConfigInfo>());
                        }

                        if (!this.configInfos[area].ContainsKey(device))
                        {//在区域配置列表中添加设备配置文件
                            this.configInfos[area].Add(device, configInfo);
                        }
                    }
                }
            }
            else
            {
                dirConfig.Create();
            }

        }

        #region IConfig 成员
        //单例模式
        public static IConfigReader SingleConfig
        {
            get
            {
                if (_config == null)
                {
                    _config = new FileConfigReader();
                    return _config;
                }
                else
                    return _config;
            }
        }

        /// <summary>
        /// 参数索引器
        /// </summary>
        /// <param name="area">区域名</param>
        /// <param name="device">设备名</param>
        /// <param name="parameter">参数名</param>
        /// <returns></returns>
        public string this[string area, string device, string parameter]
        {
            get { return configInfos[area][device][parameter]; }
        }

        /// <summary>
        /// 配置信息索引器
        /// </summary>
        /// <param name="area">区域名</param>
        /// <param name="device">设备名</param>
        /// <returns></returns>
        public IConfigInfo this[string area, string device]
        {
            get { return configInfos[area][device]; }
        }

        public Dictionary<string, List<string>> GetAreaDeviceList()
        {
            Dictionary<string, List<string>> areaDeviceList = new Dictionary<string, List<string>>();

            foreach (KeyValuePair<string, Dictionary<string, FileConfigInfo>> area in this.configInfos)
            {
                List<string> devices = new List<string>();

                foreach (string device in area.Value.Keys)
                {
                    devices.Add(device);
                }

                areaDeviceList.Add(area.Key, devices);

            }
            return areaDeviceList;
        }

        #endregion
    }
}
